<template>
  <div class="marketList">
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <div slot="header">
        <el-button v-btn-permi:add type="primary" size="default" @click="dialogVisible = true">新增</el-button>

      </div>
      <div class="table-holder">
        <!-- 展示表格 -->
        <el-table :data="activeList" border style="width: 100%">
          <el-table-column fixed="left" type="index" label="序号" width="50" />
          <el-table-column label="商品图片" width="200">
            <template slot-scope="scope">
              <img class="table-image " :src="scope.row.pic" alt="">
            </template>
          </el-table-column>
          <el-table-column label="活动时间" width="300">
            <template slot-scope="scope">
              <p>开始时间：{{ scope.row.promotionStartTime }}</p>
              <p>结束时间：{{ scope.row.promotionEndTime }}</p>
            </template>
          </el-table-column>
          <el-table-column label="是否过期" width="200">
            <template slot-scope="scope">
              <p v-if="overdue(scope.row.promotionEndTime)">已过期</p>
              <p v-else>未过期</p>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="商品名称" width="200" />
          <el-table-column prop="brandName" label="品牌名称" width="200" />
          <el-table-column prop="promotionPrice" label="商品价格" width="200" />
          <el-table-column prop="productCategoryName" label="商品类别" width="200" />
          <el-table-column prop="createTime" label="创建时间" width="200" />
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                v-btn-permi:remove
                type="text"
                style="color:red"
                size="small"
                @click="remove(scope.row.recommendId)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <div class="pop">
      <el-dialog title="热销详情" width="80%" :visible.sync="dialogVisible">
        <el-card shadow="always" :body-style="{ padding: '20px' }">
          <div slot="header">
            <span>条件查询</span>
          </div>
          <!-- 搜索输入框 -->
          <div class="search-holder">
            <el-row :gutter="20">
              <el-col :span="4">
                <el-input v-model="searchVal.name" placeholder="商品名称/模糊查询" />
              </el-col>
              <el-col :span="4">
                <el-input v-model="searchVal.productSn" placeholder="商品货号" />
              </el-col>
              <el-col :span="4">
                <el-select v-model="searchVal.brandId" placeholder="品牌">
                  <el-option
                    v-for="item in brandList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-col>
              <el-col :span="4">
                <el-select v-model="searchVal.publishStatus" placeholder="上架状态">
                  <el-option
                    v-for="item in shelvesStatus"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-col>
              <el-col :span="4">
                <el-select v-model="searchVal.verifyStatus" placeholder="审核状态">
                  <el-option
                    v-for="item in examineStatus"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-col>
              <el-col :span="4">

                <el-button @click="resetForm">重置</el-button>
                <el-button type="primary" @click="search">查询</el-button>

              </el-col>
            </el-row>

          </div>
        </el-card>

        <el-card shadow="always" :body-style="{ padding: '20px' }">
          <div class="table-box">
            <!-- 商品展示表格 -->
            <el-table :data="productList" border style="width: 100%">
              <el-table-column fixed="left" type="index" label="#" width="50" />
              <el-table-column label="选择" width="200">
                <template scope="scope">
                  <el-radio v-model="radio" :label="scope.$index" @change.native="changeActive(scope.row)">
                    {{ "" }}
                  </el-radio>
                </template>
              </el-table-column>
              <el-table-column label="商品图片" width="200">
                <template slot-scope="scope">
                  <img class="image" :src="scope.row.pic" alt="">
                </template>
              </el-table-column>
              <el-table-column prop="name" label="商品名称" width="200" />
              <el-table-column prop="price" label="商品价格" width="200" />

              <el-table-column prop="weight" label="重量" />
            </el-table>
          </div>
          <!-- 分页器 -->
          <div class="pagination-holder">
            <el-pagination
              :current-page="page.start"
              :page-sizes="page.pageSize"
              :page-size="page.limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-card>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-dialog
          width="70%"
          title="编辑限时活动"
          :visible.sync="innerVisible"
          append-to-body
        >
          <el-form label-width="80px" :model="changeInfo">
            <el-row :gutter="20">
              <el-col :span="12" :offset="0">
                <el-form-item label="商品原价">
                  {{ changeInfo.price }}
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="0">
                <el-form-item label="商品名称">
                  {{ changeInfo.name }}
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12" :offset="0">
                <el-form-item label="促销价格">
                  <el-input-number v-model="changeInfo.promotionPrice" :min="0" />
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="0">
                <el-form-item label="排序">
                  <el-input-number v-model="changeInfo.sort" :min="0" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12" :offset="0">
                <el-form-item label="开始时间">
                  <el-date-picker
                    v-model="changeInfo.promotionStartTime "
                    type="datetime"
                    placeholder="选择日期时间"
                    align="right"
                    value-format="yyyy-MM-dd HH:mm:ss"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="0">
                <el-form-item label="结束时间">
                  <el-date-picker
                    v-model="changeInfo.promotionEndTime "
                    type="datetime"
                    placeholder="选择日期时间"
                    align="right"
                    value-format="yyyy-MM-dd HH:mm:ss"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12" :offset="0">
                <el-form-item label="限购数量">
                  <el-input-number v-model="changeInfo.promotionPerLimit" :min="0" />
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="0">
                <el-form-item label="商品图片">
                  <img style="width:200px" :src="changeInfo.pic" alt="">
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8" :offset="12">
                <el-form-item>
                  <el-button @click="innerVisible=false">取消</el-button>
                  <el-button type="primary" @click="createActive">确定</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-dialog>
      </el-dialog>
    </div>

  </div>
</template>

<script>
import {
  requestActiveList,
  deleteActive,
  addRecommend
} from '@/api/market'

import {
  requestProductList,
  requestBrandList
} from '@/api/product'

import mixin from '@/mixins/index'
export default {
  mixins: [mixin],
  data() {
    return {
      // 修改活动信息
      changeInfo: {},
      radio: [],
      // 限时活动列表
      activeList: [],
      // 品牌列表
      brandList: [],
      // 商品列表
      productList: [],
      // 上架状态列表
      shelvesStatus: [
        { name: '下架', value: 0 },
        { name: '上架', value: 1 }
      ],
      // 审核状态列表
      examineStatus: [
        { name: '未审核', value: 0 },
        { name: '审核通过', value: 1 }
      ],
      // 搜索信息
      searchVal: {
        'brandId': '',
        'id': '',
        'name': '',
        'productCategoryId': '',
        'productSn': '',
        'publishStatus': '',
        'verifyStatus': ''
      },
      // 弹出框展示
      dialogVisible: false,
      // 内层弹出框显示
      innerVisible: false
    }
  },
  created() {
    this.getProduct()
    this.getData()
    this.getBrandList()
  },
  methods: {
    // 判断是否过期
    overdue(str) {
      const now = new Date()
      const time = new Date(str)
      return now.getTime() > time.getTime()
    },
    // 重置搜索结果
    resetForm() {
      this.searchVal = {
        'brandId': '',
        'id': '',
        'name': '',
        'productCategoryId': '',
        'productSn': '',
        'publishStatus': '',
        'verifyStatus': ''
      }
      this.getProduct()
    },
    // 搜索
    search() {
      console.log(this.searchVal)
      this.page.start = 1
      this.getProduct()
    },
    // 获取用户列表信息
    getData() {
      requestActiveList().then((res) => {
        // console.log(res)
        this.activeList = res.data.items
      })
    },
    // 获取商品列表
    getProduct() {
      requestProductList(this.page, this.searchVal).then(res => {
        console.log(res)
        this.productList = res.data.rows
        this.page.total = res.data.total
      })
    },
    // 获取品牌列表
    getBrandList() {
      requestBrandList().then(res => {
        // console.log(res)
        this.brandList = res.data.items
      })
    },
    // 改变每页数量
    handleSizeChange(val) {
      this.page.limit = val
      this.getProduct()
    },
    // 改变页码
    handleCurrentChange(val) {
      this.page.start = val
      this.getProduct()
    },
    // 编辑
    changeActive(item) {
      console.log(item)
      this.changeInfo = item
      this.changeInfo.productId = item.id
      this.innerVisible = true
    },
    // 创建活动
    createActive() {
      // this.changeInfo.promotionStartTime = this.changeInfo.promotionStartTime.toISOString()
      // this.changeInfo.promotionEndTime = this.changeInfo.promotionEndTime.toISOString()
      console.log(this.changeInfo)
      addRecommend(this.changeInfo).then(res => {
        console.log(res)
        if (res.code === 20000) {
          this.$message({
            message: '新增活动成功',
            type: 'success'
          })
          this.innerVisible = false
          this.dialogVisible = false
          this.getData()
        }
      })
    },
    // 删除
    remove(id) {
      console.log(id)
      const _this = this
      this.$confirm('此操作将永久删除该活动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteActive(id).then((res) => {
            // console.log(res);
            if (res.code === 20000) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              _this.getData()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.marketList{
  .table-image {
    width: 100px;
    height: 100px;
  }

  .image{
    width: 100px;
    height: 100px;
  }

  .table-box{
    width: 100%;
    height: 300px;
    overflow: auto;
  }

  .goodsImg{
     width: 200px;
    height: 200px;
  }
}
</style>
